//====================================================//
//========Eric Muehlberg==============================//
//====================================================//
//========Phoenix Engine==============================//
//====================================================//

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#include "Player.h"
#include "Tile.h"
#include "Map.h"
#include "HUD.h"
#include "Bullet.h"
#include "Camera.h"
#include <string>
#include <sstream>
#include "engine.h"

Player p1 = Player(0, 0, 30, 30, "playerspritesheet.png", 9, 5);
engine e = engine();

/*
 * Update loops
 */
void MainMenuhandle()
{
	while(SDL_PollEvent(&e.event))
	{
		if(e.event.type == SDL_QUIT)
			e.quit = true;
	}

	//update objects

	//Timer update
	e.timer.Start();

	//Update Graphics
}

void Menuhandle()
{
	while(SDL_PollEvent(&e.event))
	{
		if(e.event.type == SDL_QUIT)
			e.quit = true;

		Uint8 *keystate = SDL_GetKeyState(NULL);
		 if(keystate[SDLK_h])
		 {
			 e.setState(GAMENORMAL);
		 }
		 if(keystate[SDLK_v])
		 {
			 //Back in menu or exit if menu at base menu state
			 e.setState(GAMENORMAL);
		 }
		 if(keystate[SDLK_x])
		 {
			 //Act on selection
		 }
	}

	//update objects

	//Timer update
	e.timer.Start();

	//Update Graphics
}

void Gamehandle()
{
	while(SDL_PollEvent(&e.event))
	{
		if(e.event.type == SDL_QUIT)
			e.quit = true;

		Uint8 *keystate = SDL_GetKeyState(NULL);

		if(keystate[SDLK_p])
		{
			e.setState(GAMEPAUSED);
		}
		if(keystate[SDLK_UP])
		{
			p1.facing = p1.Direction.Up;
			//Look up
			//Climb up ledge
		}
		if(keystate[SDLK_DOWN])
		{
			p1.facing = p1.Direction.Down;
			//Look down
			//Drop from ledge
		}
		if(keystate[SDLK_RIGHT])
		{
			p1.facing = p1.Direction.Right;
			if(!p1.hang)
			{
				if(!p1.MovingRight)
				{
					p1.MovingRight = true;
					p1.MovingLeft = false;
				}
			}
		}
		if(keystate[SDLK_LEFT])
		{
			p1.facing = p1.Direction.Left;
			if(!p1.hang)
			{
				if(!p1.MovingLeft)
				{
					p1.MovingLeft = true;
					p1.MovingRight = false;
				}
			}
		}
		if(keystate[SDLK_c])
		{
			if(p1.canjump)
			{
				p1.canjump = false;
				p1.jumping = true;
				p1.dy = -380;
			}
			//Jump
		}
		if(keystate[SDLK_v])
		{
			if(p1.canshoot)
			{
				p1.canshoot = false;
				//Create bullet objects
			}
		}

		if(keystate[SDLK_x])
		{
			if(p1.facing = p1.Direction.Left)
			{
				p1.facing = p1.Direction.Upleft;
			}
			else if(p1.facing = p1.Direction.Right)
			{
				p1.facing = p1.Direction.Upright;
			}
		}
		if(!keystate[SDLK_x])
		{
			if(p1.facing = p1.Direction.Upleft)
			{
				p1.facing = p1.Direction.Left;
			}
			else if(p1.facing = p1.Direction.Upright)
			{
				p1.facing = p1.Direction.Right;
			}
		}
		if(keystate[SDLK_b])
		{
			if(p1.facing = p1.Direction.Left)
			{
				p1.facing = p1.Direction.Downleft;
			}
			else if(p1.facing = p1.Direction.Right)
			{
				p1.facing = p1.Direction.Downright;
			}
		}
		if(!keystate[SDLK_b])
		{
			if(p1.facing = p1.Direction.Downleft)
			{
				p1.facing = p1.Direction.Left;
			}
			else if(p1.facing = p1.Direction.Downright)
			{
				p1.facing = p1.Direction.Right;
			}
		}

		if(keystate[SDLK_h])
		{
			//e.setState(GAMEMENU);
		}
	}

	//Update objects

	//Timer update
	e.timer.Start();

	//Update graphics
}

void Pausehandle()
{
	//Can only unpause or quit
	while(SDL_PollEvent(&e.event))
	{
		if(e.event.type == SDL_QUIT)
			e.quit = true;

		Uint8 *keystate = SDL_GetKeyState(NULL);

		if(keystate[SDLK_p])
		{
			e.setState(GAMENORMAL);
		}
	}

	//only update pause animation

	//Timer update
	e.timer.Start();

	//update graphics
}

int main(int argc, char* args[])
{
	if(e.Init() == false)
		return 1;

	e.background = e.load_image("background.png");

	//Init HUD, Camera, and Player
	//Character player = Character(200, 200, 30, 30, e.load_image("playerspritesheet.png"), 9, 5);
	//HUD HUD(10, 10, e.load_image("HUDSpritesheet.png"));
	//Camera Camera(0, 0, 300, 200, 640, 480);
	//Bullet bullet = Bullet(0, 0, 0, 0, NULL, 0);
	ASprite AnimCheck = ASprite(200, 200, 20, 20, e.load_image("testsheet.png"), 3, 3);
	AnimCheck.setFramerate(1);

	//Load Map
	//Map level1 = Map();
	//if((level1.Load_Map("testmap.txt", e.load_image("Tile.png"))) == false)
		//return 1;
	//levelmusic = Mix_LoadMUS("1.ogg");
	//if(levelmusic == NULL)
		//return 1;

	//if(Mix_PlayMusic(levelmusic, -1) == -1)
	//	return 1;

	e.timer.Start();

	while(e.quit == false)
	{
		if(e.state == MAINMENU || e.state == NEWGAME || e.state == CONTINUE)
		{
			MainMenuhandle();
		}
		else if(e.state == GAMENORMAL)
		{
			Gamehandle();
		}
		else if(e.state == GAMEMENU)
		{
			Menuhandle();
		}
		else if(e.state == GAMEPAUSED)
		{
			Pausehandle();
		}

	/*	for(int i = 0; i < 400; i++)
		{
				if(CollisionCheck(player.CollBox, level1.level[i].CollBox) == true)
					{
						SDL_Rect Left = SDL_Rect();
						SDL_Rect Right = SDL_Rect();
						SDL_Rect Top = SDL_Rect();
						SDL_Rect Bottom = SDL_Rect();

						Left.x = level1.level[i].x;
						Left.y = level1.level[i].y + 2;
						Left.w = 5;
						Left.h = level1.level[i].h - 4;

						Right.x = level1.level[i].x + level1.level[i].w - 5;
						Right.y = level1.level[i].y + 2;
						Right.w = 5;
						Right.h = level1.level[i].h - 4;

						Top.x = level1.level[i].x + 5;
						Top.y = level1.level[i].y;
						Top.w = level1.level[i].w - 10;
						Top.h = 5;

						Bottom.x = level1.level[i].x + 5;
						Bottom.y = level1.level[i].y + level1.level[i].h - 5;
						Bottom.w = level1.level[i].w - 10;
						Bottom.h = 5;

						if(CollisionCheck(player.CollBox, Left) == true)
						{
							player.x = level1.level[i].x - player.w;

						}
						if(CollisionCheck(player.CollBox, Right) == true)
						{
							player.x = level1.level[i].x + level1.level[i].w;
						}
						if(CollisionCheck(player.CollBox, Top) == true)
						{
							player.y = level1.level[i].y - player.h;
							player.dy = 0;
							player.jumping = false;
							player.canjump = true;
						}
						if(CollisionCheck(player.CollBox, Bottom) == true)
						{
							player.y = level1.level[i].y + level1.level[i].h;
							player.dy = 0;
						}
					}
		}*/

		//Camera.Update((int)player.x, (int)player.y);

		//player.Render(apply_surface, screen, Camera);

	//	Tile Temp;

	/*	for(int i = 0; i < 400; i++)
		{
			Temp = level1.level[i];
			if(CollisionCheck(Camera.CameraRect, Temp.CollBox))
			{
				if(Temp.sprite != NULL)
					apply_surface(Temp.x - Camera.CameraRect.x, Temp.y - Camera.CameraRect.y, Temp.sprite, screen, NULL);
			}
		}*/

		//HUD.Render(apply_surface, screen);

		//Update the screen
		if(e.screen != NULL)
			SDL_Flip(e.screen);
	}

	Mix_HaltMusic();

	SDL_FreeSurface(e.background);
	SDL_FreeSurface(AnimCheck.sprite);
	Mix_FreeMusic(e.audio);

	Mix_CloseAudio();
	SDL_Quit();

	return 0;
}
